﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LandandSurvey.BLL;

namespace LandandSurvey
{
    public partial class AddSurveyor : System.Web.UI.Page
    {
        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !HasPermission() )
            {
                Response.Redirect( "ErrorPermission.aspx?returnurl=AddSurveyor.aspx?id=" + Request[ "id" ] );
            }
        }

        public bool HasPermission()
        {
            if ( Session[ "user.loggedin" ] == null )
            {
                return false;
            }
            if ( Session[ "user.loggedin" ].ToString() == "true" )
            {
                landsdbEntities container = new landsdbEntities();
                string username = Session[ "user.name" ].ToString();
                AppUser user = ( from i in container.AppUsers
                                 where i.UserName == username
                                 select i ).ToList().FirstOrDefault();
                if ( user != null )
                {
                    if ( user.UploadFiles == true )
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        protected void btnSave_Click( object sender, EventArgs e )
        {
            Manager manager = new Manager();
            manager.AddSurveyor( new Surveyor() {
                Id = Convert.ToInt32( txtId.Text ),
                Name = txtName.Text,
                Status = true
            } );
            Response.Redirect( "Surveyors.aspx" );
        }
    }
}